﻿@model NetSurveillance.WebApp.Models.WhiteListModel
@{
    ViewBag.Title = "白名单设置";
    Layout = "~/Views/Shared/_SimpleLayout.cshtml";
}
@section css{
    <style type="text/css">
        tr
        {
            height: 102px;
        }

        td
        {
            padding-left: 80px;
        }
        .controlBottom {
            margin-top: 10px;
        }
        .showdia_qq_right_btns {
            width: 170px;
        }
    </style>
}
@section javascript
{
    <script type="text/javascript">
        $(document).ready(function() {
            $("#btn_showdia_qq_update").bind("click", function() {
                var domainText = $("#txt_white_domainName").val();
                var macText = $("#txt_white_mac").val();
                var portText = $("#txt_white_port").val();
                var ipText = $("#txt_white_ip").val();
                var domainObj = parseDomainOrMac(domainText, matchDomian);
                if (domainObj === false) {
                    alert('域名填写有错误,请您先核对后填写');
                    return false;
                }
                var macObj = parseDomainOrMac(macText, matchMac);
                if (macObj === false) {
                    alert('MAC地址填写有错误,请您先核对后填写');
                    return false;
                }
                var portObj = parsePortOrIp(portText, matchPort, 'port');
                if (portObj === false) {
                    alert('端口填写有错误,请您先核对后填写');
                    return false;
                }
                var ipObj = parsePortOrIp(ipText, matchIp, 'ip');
                if (ipObj === false) {
                    alert('IP地址填写有错误,请您先核对后填写');
                    return false;
                }
                var wlModel = {};
                wlModel['Domains'] = domainObj;
                wlModel['Macs'] = macObj;
                wlModel['Ports'] = portObj;
                wlModel['Ips'] = ipObj;
                $("#whiteListModel").val(JSON.stringify(wlModel));
                $("form")[0].submit();
            });

            $("#btn_showdia_complex_save").bind("click", function() {
                var hidWhiteListDomains = $("#txt_white_domainName").val();
                var hidWhiteListMacs = $("#txt_white_mac").val();
                var hidWhiteListPorts = $("#txt_white_port").val();
                var hidWhiteListIps = $("#txt_white_ip").val();
                var whiteListModelData = hidWhiteListDomains + "θ" + hidWhiteListMacs + "θ" + hidWhiteListPorts + "θ" + hidWhiteListIps;
                $("#whiteListModel").val(whiteListModelData);
                $("form").attr("action", '@Url.Action("SelectHosts")');
                $("form")[0].submit();
            });

            $('#btn_showdia_qq_exit').click(windowClose);
        });

        function splitText(textStr) {
            if (textStr.indexOf("\r\n") > 0) {
                return textStr.split("\r\n");
            } else if (textStr.indexOf("\r") > 0) {
                return textStr.split("\r");
            } else if (textStr.indexOf("\n") > 0) {
                return textStr.split("\n");
            } else {
                var arr = [];
                arr.push(textStr);
                return arr;
            }
        }

        //返回false表示验证未通过
        function parseDomainOrMac(text, matchFun) {
            if (!text) return null;
            var textArr = splitText(text);
            var objArr = [];
            var isValid = true;
            $.each(textArr, function() {
                if (matchFun.call(null, this))
                    objArr.push(this);
                else {
                    isValid = false;
                    return false;
                }
            });
            return isValid ? objArr : false;
        }

        function parsePortOrIp(text, matchFun, type) {
            if (!text) return null;
            var textArr = splitText(text);
            var objArr = [];
            var isValid = true;
            $.each(textArr, function () {
                if (this == '') return true;
                var obj = {};
                if (this.indexOf('-') > 0) {
                    var splitArr = this.split('-');
                    if (splitArr.length != 2 || !matchFun.call(null, splitArr[0]) || !matchFun.call(null, splitArr[1])) {
                        isValid = false;
                        return false;
                    }
                    if (type == 'port') {
                        obj['PortStart'] = splitArr[0];
                        obj['PortEnd'] = splitArr[1];
                    } else {
                        obj['IpStart'] = splitArr[0];
                        obj['IpEnd'] = splitArr[1];
                    }
                } else {
                    if (!matchFun.call(null, this)) {
                        isValid = false;
                        return false;
                    }
                    if (type == 'port')
                        obj['PortStart'] = this;
                    else
                        obj['IpStart'] = this;
                }
                objArr.push(obj);
            });
            return isValid ? objArr : false;
        }

        function matchDomian(domainText) {
            var regName = "^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$";
            return domainText.match(regName);
        }

        function matchMac(macText) {
            var regName = "^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$";
            return macText.match(regName);
        }

        function matchPort(portText) {
            var regName = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
            return portText.match(regName);
        }

        function matchIp(ipValue) {
            var pattern = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
            return ipValue.match(pattern);
        }

        function windowClose() {
            if (!window.confirm('@Resources.MessageResource.ExitConfirm')) return;
            window.close();
        }
    </script>
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <div class="showdia_qq_content">
        <div class="showdia_qq_middle_right" style="width: 100%;">
            <div>
                <div class="showdialog_qq_right_title" style="width: 80%;">&nbsp;&nbsp;白名单设置</div>
                <div id="tab_content" style="width: 80%;">
                    <table id="tab_list" cellpadding="0" cellspacing="0" style="border: none;">

                        <tr>
                            <td>域名</td>
                            <td>
                                @Html.TextArea("txt_white_domainName",
                                               ViewBag.Domians != null ? ViewBag.Domians as string : "", 5, 40, null)
                            </td>
                        </tr>
                        <tr>
                            <td>MAC地址</td>
                            <td>
                                @Html.TextArea("txt_white_mac", 
                                               ViewBag.Macs != null ? ViewBag.Macs as string : "", 5, 40, null)
                            </td>
                        </tr>
                        <tr>
                            <td>端口</td>
                            <td>
                                @Html.TextArea("txt_white_port",
                                               ViewBag.Ports != null ? ViewBag.Ports as string : "", 5, 40, null)
                            </td>
                        </tr>
                        <tr>
                            <td>IP地址</td>
                            <td>
                                @Html.TextArea("txt_white_ip", ViewBag.Ips != null ? ViewBag.Ips as string : "", 5, 40, null)
                            </td>
                        </tr>
                    </table>
                </div>
                <dl class="showdia_qq_right_btns">
                    <dd style="height: 70%;"></dd>
                    <dd>
                        <input type="button" value="选择主机" id="btn_showdia_complex_save" />
                    </dd>
                </dl>
            </div>
            <dl class="showdia_qq_right_bottom">
                <dd style="width: 725px; margin-bottom: 0;">说明：</dd>
                <dd style="width: 725px;">1.监控选择中打钩主机为被监控主机<br />
                    2."白名单设置"中的内容不被监控。输入内容为域名,MAC地址,IP地址,端口,每行输入一个信息，以回车分开。如果输入一条规则,请在每行以"-"分隔
                    如：IP范围192.168.1.100到192.168.1.200时,可设置为"192.168.1.100-192.168.1.200",端口范围100到2000时,可写成"100-2000"。<br />
                    3.监控选择与白名单有冲突时,则以白名单为主。
                </dd>
            </dl>
        </div>
        <div class="ui-helper-clearfix"></div>
        @Html.Hidden("hid_whiteList_domains")
        @Html.Hidden("hid_whiteList_macs")
        @Html.Hidden("hid_whiteList_ports")
        @Html.Hidden("hid_whiteList_ips")
        @Html.Hidden("whiteListModel")
        <div class="controlBottom">
            <div class="rightContain">
                <input type="button" value="@Resources.Resource.Button_Update" id="btn_showdia_qq_update" />
                <input type="button" value="@Resources.Resource.Button_Cancel" id="btn_showdia_qq_exit" />
            </div>
        </div>
    </div>
}
